package com.lonely.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lonely.common.core.domain.api.R;
import com.lonely.system.domain.dto.menu.BatchDeleteMenuDTO;
import com.lonely.system.domain.dto.menu.MenuSubmitDTO;
import com.lonely.system.domain.dto.menu.QueryButtonDto;
import com.lonely.system.domain.dto.menu.SysMenuPageQueryDto;
import com.lonely.system.domain.vo.system.menu.QueryRoleButtonVo;
import com.lonely.system.domain.vo.system.menu.SysMenuPageQueryVo;
import com.lonely.system.domain.vo.system.menu.SysMenuTreeVo;
import com.lonely.system.service.ISysMenuService;
import com.lonely.system.service.ISysRoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 *
 * @author fzzf
 */
@RestController
@RequestMapping("/systemManage/menu")
@Api(tags = "系统管理/菜单管理")
public class SysMenuController {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    /**
     * 菜单分页查询列表
     *
     * @param queryDto
     * @return
     */
//    @SaCheckPermission("system:menu:page")
    @PostMapping("/page")
    @ApiOperation("分页查询")
//    @SaCheckPermission("system:menu:page")
    public R<Page<SysMenuPageQueryVo>> page(@RequestBody SysMenuPageQueryDto queryDto) {
        return R.data(this.menuService.page(queryDto));
    }

    /**
     * 获取菜单树,排除了按钮
     *
     * @return
     */
    @GetMapping("/getMenuTree")
    @ApiOperation("获取菜单树")
    public R<List<SysMenuTreeVo>> getMenuTree() {
        return R.data(this.menuService.getMenuTree());
    }


    /**
     * 获取角色id对应的选中的菜单下的 所有按钮集合,(即使有些按钮没有选中，但是也需要返回)
     *
     * @return
     */
    @GetMapping("/getButtonsFromSelectedMenuByRoleId/{roleId}")
    @ApiOperation("获取角色id对应的按钮权限集合")
    public R<List<QueryRoleButtonVo>> getButtonsFromSelectedMenuByRoleId(@PathVariable("roleId") Long roleId) {
        return R.data(this.sysRoleMenuService.getButtonsFromSelectedMenuByRoleId(roleId));
    }


    /**
     * 根据角色id获取角色绑定的菜单id集合
     *
     * @return
     */
    @GetMapping("/getRoleMenusByRoleId/{roleId}")
    @ApiOperation("根据角色id获取角色绑定的菜单id集合")
    public R<List<Long>> getRoleMenusByRoleId(@PathVariable("roleId") Long roleId) {
        return R.data(this.sysRoleMenuService.getRoleMenusByRoleId(roleId));
    }

    /**
     * 根据角色id获取角色绑定的按钮id集合
     *
     * @return
     */
//    @SaCheckPermission("system:menu:getRoleButtonsByRoleId")
    @GetMapping("/getRoleButtonsByRoleId/{roleId}")
    @ApiOperation("根据角色id获取角色绑定的按钮id集合")
    public R<List<Long>> getRoleButtonsByRoleId(@PathVariable("roleId") Long roleId) {
        return R.data(this.sysRoleMenuService.getRoleButtonsByRoleId(roleId));
    }


    /**
     * 查询指定菜单下的按钮集合
     *
     * @return
     */
//    @SaCheckPermission("system:menu:getButtonsByMenuIds")
    @PostMapping("/getButtonsByMenuIds")
    @ApiOperation("查询指定菜单下的按钮集合")
    public R<List<QueryRoleButtonVo>> getButtonsByMenuIds(@RequestBody QueryButtonDto queryButtonDto) {
        return R.data(this.menuService.getButtonsByMenuIds(queryButtonDto));
    }

    /**
     * 保存/编辑 菜单
     *
     * @param submitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("保存/编辑菜单")
    @SaCheckPermission("system:menu:submit")
    public R<Boolean> submit(@RequestBody @Validated MenuSubmitDTO submitDTO) {
        this.menuService.submit(submitDTO);
        return R.status(true);
    }

    /**
     * 删除菜单
     *
     * @param menuId
     * @return
     */
    @GetMapping("/deleteMenu/{menuId}")
    @ApiOperation("删除菜单")
    @SaCheckPermission("system:menu:deleteMenu")
    public R<Boolean> deleteMenu(@PathVariable("menuId") Long menuId) {
        this.menuService.deleteMenu(menuId);
        return R.status(true);
    }

    /**
     * 批量删除菜单
     *
     * @param deleteMenuDTO
     * @return
     */
    @PostMapping("/batchDeleteMenu")
    @ApiOperation("批量删除菜单")
    public R<Boolean> batchDeleteMenu(@RequestBody @Validated BatchDeleteMenuDTO deleteMenuDTO) {
        this.menuService.batchDeleteMenu(deleteMenuDTO);
        return R.status(true);
    }


}